package com.ewins.weatherdataclient.downloadmodule;

/**
 * Created by lenovo on 2015/12/4.
 */
import org.apache.log4j.Logger;

import java.io.*;

public class FileAccessI implements Serializable {
    private static final long serialVersionUID = -6861188956464817132L;
    RandomAccessFile oSavedFile;
    long nPos;
    Logger log=Logger.getLogger(FileAccessI.class);

    public FileAccessI() throws IOException {
        this("", 0);
    }

    public FileAccessI(String sName, long nPos) throws IOException {
        oSavedFile = new RandomAccessFile(sName, "rw");
        this.nPos = nPos;
        oSavedFile.seek(nPos);
    }

    public synchronized int write(byte[] b, int nStart, int nLen) {
        int n = -1;
        try {
            oSavedFile.write(b, nStart, nLen);
            n = nLen;
        } catch (IOException e) {
            log.error("File writing error: "+e.toString());
            //e.printStackTrace();
        }
        return n;
    }

    public synchronized int close() {
        int n = -1;
        try {
            oSavedFile.close();
        } catch (IOException e) {
            log.error("File writing error: "+e.toString());
            //e.printStackTrace();
        }
        return n;
    }
}